﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlueLotus7.ExternalApi.TencentStatusApi;
using BlueLotus7.ArchiveSystem.Common;
using BlueLotus7.ArchiveSystem.WCFClients;
using System.Threading;

namespace BlueLotus7.ArchiveSystem
{
    public class StatusMonitorContainer
    {
        protected Guid clientKey;
        protected List<StatusMonitor> monitors = new List<StatusMonitor>();

        public StatusMonitorContainer(Guid clientKey)
        {
            this.clientKey = clientKey;
            Initialize();
        }

        protected void Initialize()
        {
            using (ArchiveStatusServiceClient client = Global.ServiceClient)
            {
                try
                {
                    List<DomainScope> domainScopes = client.QueryDomainScopeList(null, null);
                    InitMonitors(domainScopes);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    client.Close();
                }
            }
        }

        protected void InitMonitors(List<DomainScope> domainScopes)
        {
            foreach (var one in domainScopes)
            {
                StatusMonitor monitor = null;
                switch (one.Code)
                {
                    case TencentStatusMonitor.scopeCode:
                        monitor = new TencentStatusMonitor(one.Key, this.clientKey);
                        break;
                    default:
                        break;
                }

                if (monitor != null)
                {
                    monitors.Add(monitor);
                }
            }


        }

        public void Start()
        {
            foreach (var one in this.monitors)
            {
                one.Start();
            }
        }

        public void End()
        {
            foreach (var one in this.monitors)
            {
                one.Dispose();
            }
        }
    }
}
